package com.delcol.Email_Class;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
import java.util.InputMismatchException;

public class Email_Class {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int guesscounter = 10;
		int Hitcounter = 0;
		int Spacesleft = 3;
		int counter = 0;
		int playerhit = 0;
		String enemyguess;
		String playerspace[] = new String[3];
		String[] enemypostition = new String[3];
		Random rand = new Random();
		System.out.print("Welcome to the Battleship game");
		System.out
				.println(" Here are the rules: You must select slots out of a 4 slots of the enemy boat to destroy it you have a three slot boat ");
		System.out
				.println("These are the options for placing your boat and attacking the other boat: A1, A2, A3, A4, B1, B2, B3, B4,C1, C2, C3, C4, D1, D2, D3, D4");
		String[] position = { "A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4",
				"C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4" };

		enemypostition[0] = position[rand.nextInt(position.length)];
		enemypostition[1] = position[rand.nextInt(position.length)];
		enemypostition[2] = position[rand.nextInt(position.length)];
		System.out.println(Arrays.toString(enemypostition));
		do {
			for (int x = 0; x < 3; x++) {
				System.out.println("Insert the position of your 3 space boat");

				try {

					playerspace[x] = scan.nextLine();

				} catch (InputMismatchException e) {
					System.out.println("That is not a choice");

				}
				System.out.println(playerspace[x]);
				counter++;

			}
		} while (counter < 3);

		System.out.println("Your Boat has been set");

		System.out.println("Take Your first guess");
		do {
			String x = scan.nextLine();
			if (x.equals(enemypostition[0]) || x.equals(enemypostition[1])
					|| x.equals(enemypostition[2])) {

				System.out.println("Good shot captain!");
				Hitcounter++;
				Spacesleft--;
				System.out.println("You have " + Spacesleft + "left to hit");

			}else{
				System.out.println("You have missed");
			}

			System.out.println("Its the enemys turn to guess");

			System.out.println(Arrays.toString(playerspace));
			enemyguess = position[rand.nextInt(position.length)];
			System.out.println("The enemy guessed " + enemyguess);
			if (enemyguess.equals(playerspace[0])
					|| enemyguess.equals(playerspace[1])
					|| enemyguess.equals(playerspace[2])) {
				System.out.println("You have been hit!");
				playerhit++;
				
			}else{
				System.out.println("The enemy has missed");
			}
		
		} while (playerhit < 3 && Hitcounter < 3);

		
		if (Hitcounter == 3) {
			System.out.println("You have won!");
			Spacesleft--;

		}
		else if (playerhit == 3) {
			System.out.println("Game over");
			scan.close();
		}

	}

}
